package com.iteaj.iot.client.codec;

import com.iteaj.iot.SocketMessage;
import com.iteaj.iot.client.ClientConnectProperties;
import com.iteaj.iot.client.TcpSocketClient;
import com.iteaj.iot.client.ClientComponent;
import com.iteaj.iot.codec.adapter.ByteToMessageDecoderAdapter;
import com.iteaj.iot.config.ConnectProperties;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

public class ByteToMessageDecoderClient extends TcpSocketClient {

    public ByteToMessageDecoderClient(ClientComponent clientComponent, ClientConnectProperties config) {
        super(clientComponent, config);
    }

    @Override
    protected ByteToMessageDecoderAdapter createProtocolDecoder() {
        return new ByteToMessageDecoderAdapter() {

            @Override
            public SocketMessage proxy(ChannelHandlerContext ctx, ByteBuf decode) throws Exception {
                return ByteToMessageDecoderClient.this.getClientComponent().proxy(ctx, decode);
            }

            @Override
            public SocketMessage createMessage(byte[] message) {
                return ByteToMessageDecoderClient.this.getClientComponent().createMessage(message);
            }

            @Override
            public SocketMessage doDecode(ChannelHandlerContext ctx, ByteBuf in) {
                return ByteToMessageDecoderClient.this.getClientComponent().doDecode(ctx, in);
            }

            @Override
            public Class<? extends SocketMessage> getMessageClass() {
                return ByteToMessageDecoderClient.this.getClientComponent().getMessageClass();
            }
        };
    }
}
